package com.compass.doctor.ui.home.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.WebDetailActivity
import com.compass.doctor.ui.home.adapter.InquiryPatientAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * Description： 团队医生--问诊患者
 * Date:2023/12/23 15:11
 */
class InquiryPatientFragment : BaseVBFragment<FragmentBaseListBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private val patientViewModel: PatientViewModel by activityViewModels()
    private lateinit var mAdapter: InquiryPatientAdapter
    private var mPage = 0
    private var orderType: Int = 1
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            orderType = it.getInt(KEY_ORDER_TYPE)
        }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@InquiryPatientFragment)
            setOnLoadMoreListener(this@InquiryPatientFragment)
        }
        mAdapter = InquiryPatientAdapter()
        val dp10 = dpToPx(10)
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                setLastBottom(true)
            })
            adapter = mAdapter
        }


        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)
            if (item != null && !item.link.isNullOrEmpty()) {
                //跳转业务
                WebDetailActivity.start(
                    context = requireContext(),
                    url = item.link!!,
                    title = item.title ?: ""
                )
            }
        }

    }




    override fun initData() {
        getPrescribedList()
    }

    private fun getPrescribedList() {
        showLoading()


        lifecycleScope.launch {
            delay(1000)
            //延迟获取焦点
            dismissLoading()
             mBinding?.emptyView?.visible()
        }
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getPrescribedList()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 0
        getPrescribedList()
    }
}